package uto.phone.flash.wind.base


import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.util.Log
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import java.text.SimpleDateFormat
import java.util.*

//------------------------------------------------Toast提示-----------------------------------------------------------

fun Context.toast(charSequence: CharSequence?){
    charSequence ?: return
    IActivity.toast = IActivity.toast ?: Toast.makeText(applicationContext,charSequence,Toast.LENGTH_SHORT)
    IActivity.toast?.setText(charSequence)
    IActivity.toast?.show()
}

fun Context.toast(@StringRes id: Int){ toast(getString(id)) }
fun Context.toast(@StringRes id: Int,vararg any: Any){ return toast(getString(id,any)) }

fun Fragment.toast(@StringRes id: Int){ context?.toast(id) }
fun Fragment.toast(charSequence: CharSequence?){ context?.toast(charSequence) }
fun Fragment.toast(@StringRes id: Int,vararg any: Any){ context?.toast(id,any)}

fun View.toast(@StringRes id: Int){ context.toast(id) }
fun View.toast(charSequence: CharSequence?){ context.toast(charSequence) }
fun View.toast(@StringRes id: Int,vararg any: Any){ context.toast(id,any) }

fun RecyclerView.ViewHolder.toast(charSequence: CharSequence?){ itemView.toast(charSequence)}
fun RecyclerView.ViewHolder.toast(@StringRes id: Int){ itemView.toast(id)}
fun RecyclerView.ViewHolder.toast(@StringRes id: Int,vararg any: Any){ itemView.toast(id,any) }

//---------------------------------------------------View显示与隐藏-----------------------------------------------------
fun View.gone(){ visibility = View.GONE }
fun View.show(){ visibility = View.VISIBLE }
fun View.invisible(){ visibility = View.INVISIBLE }

//--------------------------------------------------日志打印------------------------------------------------------------
fun Any.i(info: String){ Log.i(this::javaClass.name,info) }
fun Any.d(info: String){ Log.d(this::javaClass.name,info) }
fun Any.v(info: String){ Log.v(this::javaClass.name,info) }
fun Any.w(info: String){ Log.w(this::javaClass.name,info) }
fun Any.e(error: String){ Log.e(this::javaClass.name,error) }


//--------------------------------------------------视图查询------------------------------------------------------------


fun <V: View> RecyclerView.ViewHolder.findView(id: Int): V{ return itemView.findViewById(id) }


//---------------------------------------------------获取db尺寸-----------------------------------------------------

fun Context.dp(size: Int): Int{ return (size * resources.displayMetrics.density + 0.5f).toInt()}
fun Fragment.dp(size: Int): Int{ return context!!.dp(size)}
fun View.dp(size: Int): Int{ return context.dp(size)}
fun RecyclerView.ViewHolder.dp(size: Int): Int{ return itemView.dp(size)}

//---------------------------------------------------获取字符串------------------------------------------------------
fun Fragment.getString(@StringRes id: Int): String{ return context!!.getString(id) }
fun Fragment.getString(@StringRes id: Int,vararg any: Any): String{ return context!!.getString(id,any) }

fun View.getString(@StringRes id: Int):String{ return context.getString(id) }
fun View.getString(@StringRes id: Int,vararg any: Any): String{ return context.getString(id,any) }

fun RecyclerView.ViewHolder.getString(@StringRes id: Int): String{ return itemView.getString(id) }
fun RecyclerView.ViewHolder.getString(@StringRes id: Int,vararg any: Any): String{ return itemView.getString(id,any) }

//----------------------------------------------------隐藏软键盘-------------------------------------------------------
fun Context.hideKeyboard(view: View){
    view.clearFocus()
    val inputManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as? InputMethodManager
    inputManager?.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
fun Fragment.hideKeyboard(view: View){ context?.hideKeyboard(view) }
fun View.hideKeyboard(view: View){ context?.hideKeyboard(view) }
fun RecyclerView.ViewHolder.hideKeyboard(view: View){ itemView.hideKeyboard(view) }

//----------------------------------------------------显示软键盘-------------------------------------------------------
fun Context.showKeyboard(view: View){
    view.requestFocus()
    val inputManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as? InputMethodManager
    inputManager?.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
fun Fragment.showKeyboard(view: View){ context?.showKeyboard(view) }
fun View.showKeyboard(view: View){ context?.showKeyboard(view) }
fun RecyclerView.ViewHolder.showKeyboard(view: View){ itemView.showKeyboard(view) }

//-----------------------------------------------------日期相关--------------------------------------------------------

/**
 * 获取给定时间到第二天的差值
 * SimpleDateFormat("yyyy-MM-dd HH:mm")
 */
fun Any.tomorrow(): Long{
    val calendar = Calendar.getInstance()
    calendar.add(Calendar.DAY_OF_YEAR,1)
    calendar.set(Calendar.HOUR_OF_DAY,0)
    calendar.set(Calendar.MINUTE,0)
    calendar.set(Calendar.SECOND,0)
    calendar.set(Calendar.MILLISECOND,0)
    return calendar.timeInMillis - System.currentTimeMillis()
}

/**
 * 格式化时间字符串
 */
@SuppressLint("SimpleDateFormat")
fun Any.timeString(time: Long, format: String = "yyyy-MM-dd HH:mm"): String{
    return SimpleDateFormat(format).format(Date(time))
}

/**
 * 窗口全屏
 */
fun Activity.fullScreen(){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
        window.statusBarColor = Color.TRANSPARENT
    }
}

/**
 * 获取状态栏高度
 */
fun Context.getStatusH() : Int{
    return resources.getIdentifier("status_bar_height","dimen","android")
}
